<?php
namespace common\service;
use common\models\SupplierAttribute;
use common\models\SupplierGoodsAttr;
use common\models\SupplierAttributeValue;

class SupplierGoodsService extends BaseService
{
    /**
     * 获取商品SKU
     * $type 1 过滤库存0
     * @param $id
     * @return mixed
     */
    public static function GetAttrlist($id){
        $lists = SupplierGoodsAttr::find();
        $lists->andWhere(['goods_id'=>$id,'is_del'=>0]);
        $list = $lists->asArray()->all();
        if($list) {
            $aid = explode('-', $list[0]['attribute_value_id']);

            $attrtitle = SupplierAttributeValue::find()->where(['id'=>$aid])->orderBy(['id' => SORT_ASC])->asArray()->all();
            $vid = array_column($list, 'attribute_value_id');
            $new_attrtitle = [];
            foreach ($attrtitle as $value){
                $new_attrtitle[$value['attr_id']] = $value['attr_name'];
            }
            unset($value);
            ksort($new_attrtitle);
            $newdata['thead'] = array_values($new_attrtitle);
            $newiddata = [];
            foreach ($vid as $val) {
                $newid = explode('-', $val);
                foreach ($newid as $vid) {
                    if ($vid > 0) {
                        $newiddata[] = $vid;
                    }
                }
            }
            $newiddata = array_unique($newiddata);

            $newvaldata = SupplierAttributeValue::find()->where(['in', 'id', $newiddata])->asArray()->all();

            $valdatanew = array();
            foreach ($newvaldata as $newdats) {
                $valdatanew[$newdats['id']] = $newdats;
            }

            $data = [];
            foreach ($list as $lval) {
                $newavid = explode('-', $lval['attribute_value_id']);
                $newvalue['original_price'] = $lval['original_price'];
                $newvalue['discount_price'] = $lval['discount_price'];
                $newvalue['inventory'] = $lval['inventory'];
                $newvalue['image'] = GlobalUrlService::buildCdnUrl($lval['image']);
                $newvalue['images'] = $lval['image'];
                $newvalue['attribute_value_id'] = $lval['attribute_value_id'];
                $newvalue['attr_id'] = $lval['id'];
                $newvalue['id'] = $lval['id'];

                $namedata = array();
                foreach ($newavid as $newval) {
                    if ($newval > 0) {
                        $namedata[$valdatanew[$newval]['attr_id']] = $valdatanew[$newval]['value'];
                    }

                }
                ksort($namedata);
                $newvalue['val'] = array_values($namedata);
                $data[] = $newvalue;
            }
            $newdata['tbody'] = $data;
            return $newdata;
        } else{
            return array();
        }
    }

    /**
     * 获取商品的价格 原价的最高价和最低价,折扣价的最高价和最低价
     * @param array $goods
     * @return array
     */
    public static function GetAttrListPrice($goods = array()){
        if(empty($goods)){
            return [];
        }

        $goods_ids = array_column($goods,'id');
        $attr_lists = SupplierGoodsAttr::find()
            ->select(['id','goods_id','original_price','discount_price'])
            ->where(['goods_id'=>$goods_ids,'is_del'=>0])
            ->asArray()
            ->all();

        $new_attr_lists = [];
        foreach ($attr_lists as $item){
            $new_attr_lists[$item['goods_id']]['original_price'][] = $item['original_price'];
            $new_attr_lists[$item['goods_id']]['discount_price'][] = $item['discount_price'];

        }
        unset($item);

        foreach ($goods as &$item){
            $item['list_img'] = GlobalUrlService::buildCdnUrl($item['list_img']);
            $item['max_original_price'] = 0;
            $item['min_original_price'] = 0;
            $item['max_discount_price'] = 0;
            $item['min_discount_price'] = 0;
            if(isset($new_attr_lists[$item['id']]) && !empty($new_attr_lists[$item['id']])){
                $original_price = $new_attr_lists[$item['id']]['original_price'];
                $discount_price = $new_attr_lists[$item['id']]['discount_price'];
                $item['max_original_price'] = max($original_price);
                $item['min_original_price'] = min($original_price);
                $item['max_discount_price'] = max($discount_price);
                $item['min_discount_price'] = min($discount_price);
            }

        }
       return $goods;
    }

    /**
     * 获取属性值
     * @param array $actives
     * @return string
     */
    public static function getAttributeValue($actives = []){
        if(empty($actives)){
            return [];
        }
        //获取所有的属性
        $attrids = '';
        $new_actives = [];
        foreach ($actives as $value){
            $new_actives[$value['id']] = $value;
            $new_actives[$value['id']]['attribute'] = $value['attribute_value_id'];
            $ids = str_replace('-',',',$value['attribute_value_id']);
            $attrids = $attrids.','.$ids ;
        }
        unset($value);
        $attrids = explode(',',$attrids);
        $attrids = array_splice($attrids,1);
        $new_attrids = array_unique($attrids);

        //获取所有属性值
        $attributes = SupplierAttributeValue::find()->where(['id'=>$new_attrids])->asArray()->all();
//        if(empty($attributes)){
//            return '';
//        }
        //组合属性名称和值
        $new_attributes = [];
        foreach ($attributes as $val){
            $new_attributes[$val['id']] = $val['attr_name'].':'.$val['value'];

        }
        unset($val);

        //组合属性id和对应的值
        foreach ($new_actives as $key=>$val){
            $attr_id = explode('-',$val['attribute']);
            $str = '';
            foreach ($attr_id as $id){
                if(isset($new_attributes[$id]) && !empty($new_attributes[$id])){
                    $str = $str.','.$new_attributes[$id];
                }
            }
            $new_actives[$key]['attribute'] = ltrim($str,',');
        }

        return $new_actives;
    }

}